﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;

namespace FileUpLoad.WebService
{
    /// <summary>
    /// WS 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WS : System.Web.Services.WebService
    {
        /// <summary>
        /// 图片删除密钥
        /// </summary>
        static string imageServicesDeleteKeyt = "71309E78";
        /// <summary>
        /// url 地址分隔符
        /// </summary>
        static char[] splitChar = { '/' };
        /// <summary>
        /// 文件保存的基本地址
        /// </summary>
        static string savePath = @"e:\www.Imgserver.com\upload\{0}";

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="text">明文</param>
        /// <param name="ciphertext">密文</param>
        [WebMethod]
        public void DeleteImageFile(string text, string ciphertext)
        {

            if (ciphertext.Equals(VCommons.Encryptor.Utility.DecryptString(text + imageServicesDeleteKeyt, VCommons.Encryptor.Utility.EncryptorType.MD5)))//验证明文和密文
            {
                string[] vals = ciphertext.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
                if (vals.Length == 2)
                {
                    string currPath = string.Format(savePath, vals[0]);
                    string fullFileName = Path.Combine(currPath, vals[1]);
                    File.Delete(fullFileName);// 删除原始图片
                    fullFileName = null;
                    currPath = null;
                }
                else
                {
                    throw new Exception("地址处理不成功");
                }
            }
            else
            {
                throw new Exception("密文处理不成功");
            }
        }
        [WebMethod]
        public string UploadImageFile(Byte[] bytes)
        {
            try
            {
                Image image = Image.FromStream(new MemoryStream(bytes));
                string currPath = string.Format(savePath, "banner");
                string fileName = Guid.NewGuid().ToString() + ".jpg";

                if (!Directory.Exists(currPath))//如果地址不存在就建立这个地址
                {
                    Directory.CreateDirectory(currPath);
                }
                using (FileStream stream = new FileStream(currPath + "\\" + fileName, FileMode.Create, FileAccess.Write))
                {
                    stream.Write(bytes, 0, bytes.Length);

                    stream.Close();
                }
                return "http://img.server.com/upload/banner/" + fileName;
            }
            catch (Exception)
            {
                return "";
            }
        }



    }
}
